﻿using Common;
using Dapr.Actors;
using Dapr.Actors.Client;
using Microsoft.AspNetCore.Mvc;

namespace FrontEnd.Controllers;

[Route("api/[controller]")]
[ApiController]
public class DaprActorsClientController : ControllerBase
{
    #region DI 注册 Logger
    
    private readonly ILogger<DaprActorsClientController> _logger;
    private readonly IActorProxyFactory _actorProxyFactory;

    public DaprActorsClientController(ILogger<DaprActorsClientController> logger, IActorProxyFactory actorProxyFactory)
    {
        _logger = logger;
        _actorProxyFactory = actorProxyFactory;
    }
    
    #endregion

    #region DI 注册 Logger
    /*
    private readonly ILogger<DaprActorsClientController> _logger;

    public DaprActorsClientController(ILogger<DaprActorsClientController> logger)
    {
        _logger = logger;
    }
    */
    #endregion

    [HttpGet("Approve/{orderId}")]
    public async Task<ActionResult> ApproveAsync(string orderId)
    {
        _logger.LogInformation($"[{DateTime.Now}] 进入 DaprActorsClient.ApproveAsync 方法");

        var proxy = CreateProxyById(orderId);

        return Ok(await proxy.ApproveAsync());
    }

    [HttpGet("Increment/{orderId}")]
    public async Task<ActionResult> IncrementAsync(string orderId)
    {
        _logger.LogInformation($"[{DateTime.Now}] 进入 DaprActorsClient.IncrementAsync 方法");

        var proxy = CreateProxyById(orderId);

        return Ok(await proxy.IncrementAsync());
    }

    #region Timer（计时器）
    [HttpGet("RegisterTimer/{orderId}")]
    public async Task<ActionResult> RegisterTimerAsync(string orderId)
    {
        _logger.LogInformation($"[{DateTime.Now}] 进入 DaprActorsClient.RegisterTimerAsync 方法");

        var proxy = CreateProxyById(orderId);
        await proxy.RegisterTimerAsync();

        return Ok("done");
    }

    [HttpGet("UnregistTimer/{orderId}")]
    public async Task<ActionResult> UnregistTimerAsync(string orderId)
    {
        _logger.LogInformation($"[{DateTime.Now}] 进入 DaprActorsClient.UnregistTimerAsync 方法");

        var proxy = CreateProxyById(orderId);
        await proxy.UnregisterTimerAsync();

        return Ok("done");
    }
    #endregion

    #region Reminder（提醒器）
    [HttpGet("RegisterReminder/{orderId}")]
    public async Task<ActionResult> RegisterReminderAsync(string orderId)
    {
        _logger.LogInformation($"[{DateTime.Now}] 进入 DaprActorsClient.RegisterReminderAsync 方法");

        var proxy = CreateProxyById(orderId);
        await proxy.RegisterReminderAsync();
        return Ok("done");
    }

    [HttpGet("UnregisterReminder/{orderId}")]
    public async Task<ActionResult> UnregistReminderAsync(string orderId)
    {
        _logger.LogInformation($"[{DateTime.Now}] 进入 DaprActorsClient.UnregistReminderAsync 方法");

        var proxy = CreateProxyById(orderId);
        await proxy.UnregisterReminderAsync();
        return Ok("done");
    }
    #endregion

    /// <summary>
    /// 创建 Actor 代理
    /// </summary>
    /// <param name="orderId">订单Id</param>
    /// <returns></returns>
    private  IWorkflowActor CreateProxyById(string orderId) 
    {
        var actorId = new ActorId($"actorPrifix-{orderId}");
        var proxy = _actorProxyFactory.CreateActorProxy<IWorkflowActor>(actorId, "WorkflowActor");
        //var proxy = ActorProxy.Create<IWorkflowActor>(actorId, "WorkflowActor");

        return proxy;
    }

}
